" vim:ft=vim
" Test the patterns for matching mode strings
" and extracting flags from them.
"

Before;
let [valid_mode, readable, writable] = object#file#patterns()

let valid_modes = [
      \ 'w', 'r', 'a',
      \ 'wb', 'rb', 'ab',
      \ "w+", "r+", "a+",
      \ "w+b", "r+b", "a+b",
      \ 'rw', 'ra', 'wr', 'ar',
      \ 'rwb', 'rab', 'wrb', 'arb',
      \]

let bad_modes = [
      \ '+', '',
      \ 'aw', 'wa',
      \ 'raw', 'rwa', 'awr', 'arw', 'war', 'wra',
      \ 'ra+', 'wr+', 'raw+', 'rb+', 'wb+',
      \ ]


Execute(Match valid_mode);
for x in valid_modes
  Assert x =~# valid_mode
endfor

for x in bad_modes
  Assert x !~# valid_mode
endfor

for x in valid_modes
  Assert x =~ readable || x =~ writable,
        \ 'a mode is either readable or writable'
endfor

Execute(Match readable);
let positive = [ 'rb', 'r', "w+", "a+", 'rw', 'ra',]
for x in positive
  Assert x =~# readable
endfor

let negative = ['w', 'a', 'ab', 'wb']
for x in negative
  Assert x !~# readable
endfor


Execute(Match writable);
let positive = [
      \ 'w', 'a', "r+", 'wr',
      \ 'rw', "r+", "w+", "a+",
      \ 'rwb', "r+", "w+", "a+",
      \]

for x in positive
  Assert x =~# writable
endfor
Assert 'r' !~# writable && 'rb' !~# writable


Execute(Throws ValueError for bad mode string);
for x in bad_modes
  AssertThrows call object#open(tempname(), g:x)
  Assert g:vader_exception =~# 'ValueError'
endfor


Execute(Throws WrongType if name or mode is not String);
let bad_args = [
      \ [ 1, [] ],
      \ [ {}, 1 ],
      \]

for x in bad_args
  AssertThrows call call("object#open", g:x)
  Assert g:vader_exception =~# 'WrongType'
endfor

"
" Extracting flags from mode
"
Execute(read_flags);
let with_binary = [
      \ 'rb', 'rb ', "r+b", "rwb",
      \]

let without_binary = [
      \ 'r', 'ra', 'rw', "r+",
      \]

for x in with_binary
  AssertEqual 'b', object#file#read_flags(x)
endfor

for x in without_binary
  AssertEqual '', object#file#read_flags(x)
endfor


Execute(write_flags);
AssertEqual 'a', object#file#write_flags('ra')
AssertEqual 'a', object#file#write_flags('a+')
AssertEqual 'ab', object#file#write_flags('rab')
AssertEqual 'ab', object#file#write_flags('a+b')
AssertEqual '', object#file#read_flags('r')
